# 读取xlsx文件：
import openpyxl, yaml
from openpyxl import cell

from common.encryptmd5 import EncryptMd5
from config import EXCEL_PATH, SHEET_NAME


class FileReader:

    @staticmethod
    def readExcelToDict(file_path=EXCEL_PATH, sheet_name=SHEET_NAME):
        # 打开新的excel文件或创建新的excel文件
        try:
            workbook = openpyxl.load_workbook(file_path)
        except FileNotFoundError:
            workbook = openpyxl.Workbook()

        print(workbook)

        # 选择或创建指定的工作表
        if sheet_name in workbook.sheetnames:
            worksheet = workbook[sheet_name]
        else:
            worksheet = workbook.create_sheet(sheet_name)

        # 当你走到这里的时候则证明你已经拿到了整个excel中的某个sheet
        data = []

        # 获取列名
        headers = [cell.value for cell in worksheet[2]]

        # print(headers)

        # 把小的数据从第三行开始
        for row in worksheet.iter_rows(min_row=3, values_only=True):
            # print("*"*10)
            # print(row)
            # print(zip(headers, row))
            each_data = dict(zip(headers, row))
            if each_data["is_true"]:
                data.append(dict(zip(headers, row)))
        workbook.close()
        return data

    @staticmethod
    def writeDataToExcel(file_path=EXCEL_PATH, sheet_name=SHEET_NAME, row=None, column=None, value=None):
        """
        写入xlxs文件
        :param column:
        :param file_path:
        :param sheet_name:
        :param row:
        :param value:
        :return:
        """
        # 打开现有的Excel文件或创建新文件
        try:
            workbook = openpyxl.load_workbook(file_path)
        except FileNotFoundError:
            workbook = openpyxl.Workbook()

        # 选择或创建指定的工作表
        if sheet_name in workbook.sheetnames:
            worksheet = workbook[sheet_name]
        else:
            worksheet = workbook.create_sheet(sheet_name)

        # 写入数据到指定行和列
        worksheet.cell(row=row, column=column).value = value

        # 保存修改后的文件
        workbook.save(file_path)

    # 一个加密一个方法
    @staticmethod
    def data_encryptmd5(data):
        """
        把对应字典中某个字段的数据加密成Md5数据
        :param data:
        :return:
        """
        new_data = {}
        try:
            for key in data:
                if key[0] == "@":
                    new_data[key[1:]] = EncryptMd5.md5_encrypt(data[key])
                else:
                    new_data[key] = data[key]
                # print(new_data)
            return new_data
        except Exception as e:
            print(e)
            return None
    # @staticmethod
    # def read_yaml(file_name=YAML_PATH):
    #     with open(YAML_PATH, "r", encoding="utf-8") as f:
    #         data = yaml.safe_load(f)
    #     return data

    # @staticmethod
    # def write_yaml(data, file_name=YAML_PATH):
    #     with open(file_name, "w", encoding="utf-8") as f:
    #         yaml.dump(data, f, allow_unicode=True)


if __name__ == '__main__':
    # data = FileReader.readExcelToDict()
    # print(data)
    # data = FileReader.read_yaml()
    # print(data)
    # FileReader.writeDataToExcel(row=3, column=11, value="aaa")
    # data1 = {
    #     "username": 18855688009,
    #     "@password": "xx958012725"
    # }
    # data2 = {
    #     "id": 7421,
    #     "username": "user_qlxwvm",
    #     "contact": {
    #         "email": "user_qlxwvm@example.com",
    #         "phone": "+1-557-826-4732"
    #     },
    #     "security": {
    #         "@password": "xx958012725",  # 符合你的要求
    #         "last_login": "2023-10-15T08:30:45Z"
    #     },
    #     "preferences": {
    #         "theme": "dark",
    #         "notifications": False
    #     }
    # }
    # print(FileReader.data_encryptmd5(data2))
    # casedata = FileReader.readExcelToDict()
    # print(casedata)
    # responseExpect = casedata[0]["responseExpect"]
    # print(eval(responseExpect))
    # FileReader.writeDataToExcel(row=39,column=1, value="[1,2,3]")
    pass